-- UIWorkshopDesc
-- Create by zouyb
-- 炼金工坊帮助界面

--UIWorkshopDesc继承自Layer
UIWorkshopDesc = class("UIWorkshopDesc", function()
    return cc.Layer:create();
end);

function UIWorkshopDesc.create(itemId)
    return UIWorkshopDesc.new(itemId);
end

local MATERIAL_STRAT_ID = 2;
local MATERIAL_END_ID = 9;
local SMALL_BG_COUNT = 4;

--构造函数
function UIWorkshopDesc:ctor(itemId)
    self:setName("UIWorkshopDesc");
    self.itemId = itemId;

    --初始化
    local node = cc.CSLoader:createNode("layout/workshop/WorkshopDesc.csb");
    self:addChild(node);
    self.node = node;

    -- 重绘
    self:redraw();

    -- 注册点击事件
    self:registerTouchEvent();

    -- 适配
    self:resize();
end

-- 重绘
function UIWorkshopDesc:redraw()
    -- 标题
    local titleLabel = findChildByName(self.node,"CT/title");
    TextStyleM.setTextStyle(titleLabel,TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_DARK_RED);
    titleLabel:setString(getLocStr("work_explain"));

    -- 炼金说明
    -- 最大累计时间
    local offlineLimit = AlchemyWorkshopM.calcMaxOffline();

    -- 将秒转换为小时
    offlineLimit    = string.format("%0.1f", offlineLimit/3600) ;

    -- 资源产出间隔
    local interval = FormulaM.invoke("GET_ALCHEMY_PRODUCE_INTERVAL", ME.user);

    -- 只显示第1/4条
    local text1 = string.format(getLocStr("workshop_rule_1"), interval);
    local text4 = string.format(getLocStr("workshop_rule_4"), offlineLimit);
    local moneyText = string.format(getLocStr("money_limit_text"), FormulaM.invoke("CALC_USER_MAX_MONEY", ME.user));
    local descText = text1 .. "\n" .. text4 .. "\n" .. moneyText;
    local descLabel = findChildByName(self.node, "CT/desc");
    TextStyleM.setTextStyle(descLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_DARK_BROWN);
    descLabel = applyString(descLabel, descText);

    -- 具体说明
    local itemDescText = "";
    for i = MATERIAL_STRAT_ID, MATERIAL_END_ID do
        -- 对应物品是否解锁
        if AlchemyWorkshopM.getStrengthenLevel(i + 3) > 0 then
            -- 其他材料，需要动态取配置
            local materialId = 1000 + i;
            local ruleId = CompoundM.getAutoCompoundRule(materialId);
            local materialInfo = CompoundM.query(ruleId, "material");
            local type = CompoundM.query(ruleId, "type");
            materialInfo = FormulaM.invoke("CALC_COMPOUND_MATERIAL", ME.user, materialInfo, type);
            local productDesc = string.format(getLocStr("res_workshop_rule_"..i), materialInfo[materialId]);
            itemDescText = itemDescText .. productDesc .. "\n";
        end
    end

    local itemDescLabel = findChildByName(self.node, "CT/desc_node/item_desc");
    TextStyleM.setTextStyle(itemDescLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_DARK_BROWN);
    itemDescLabel = applyString(itemDescLabel, itemDescText);

    -- 自适应所有组件
    local bg = findChildByName(self.node, "CT/bg");
    AlignM.adaptLayout(bg, AlignM.BG_SIZE_SMALL,
        { titleLabel, descLabel, itemDescLabel },
        { AlignM.DEFAULT_GAP, AlignM.DEFAULT_GAP, AlignM.DEFAULT_GAP },
        50, 60);
end

-- 注册点击事件
function UIWorkshopDesc:registerTouchEvent()
    -- 注册返回按钮点击事件
    local btnBack = findChildByName(self.node, "BT/btn_back");
    local function onBackClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            -- 关闭当前界面
            AudioM.playFx("button_return");
            UIMgr.getCurrentScene():removeFormByName("UIWorkshopDesc");
        end
    end
    AddBackEventHandler(self, btnBack, onBackClick);
    TextStyleM.setTextStyle(btnBack, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnBack:setTitleText(getLocStr("btn_text_back"));

    -- 注册点击任意地方关闭当前窗口事件
    local bgImg = findChildByName(self.node, "bg/bgImg");
    local function onBgBackClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            -- 关闭当前界面
            AudioM.playFx("button_return");
            UIMgr.getCurrentScene():removeFormByName("UIWorkshopDesc");
        end
    end
    bgImg:addTouchEventListener(onBgBackClick);

    -- 防止点击事件被吞掉
    local bg = findChildByName(self.node, "CT/bg");
    for i = 1, SMALL_BG_COUNT do
        local subBg = findChildByName(bg, "bg" .. i);
        if subBg ~= nil then
            subBg:setSwallowTouches(false);
        end
    end
end

-- 适配
function UIWorkshopDesc:resize()
    local node = self.node;

    -- bg居中显示并充满整个屏幕
    AlignM.fitToScreen(node, "bg");

    -- CT居中并缩放至合适大小
    AlignM.alignToCenter(node, "CT");

    -- BT置于底部并缩放至合适大小
    AlignM.alignToBottomCenter(node, "BT");
end


